
// 分类模块
const sequelize = require('../config/operateDB');
const {Sequelize, DataTypes, Op} = require('sequelize');
const {Products} = require('./product');


const Catagory = sequelize.define('catagory', {
    id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
    },
    // 分类名称
    name: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    // 父分类的ID, 如果没有则为null
    parentId: {
        type: DataTypes.TEXT,
        allowNull: true,
    }
}, {

})
Catagory.sync();


const catagoryModel = {
    async getCatagory(ctx) {
        const params = ctx.query;
        // const count = await Catagory.count();
        const {offset = 0, limit = 10, keywords = ''} = params;
        const { count, rows } = await Catagory.findAndCountAll(
            {
                where: {
                    name: {
                        [Op.like]: '%' + keywords + '%',
                    },
                },
                limit: parseInt(limit),
                offset: parseInt(offset),
                raw: true,
            }
        );

        ctx.pageRes({
            limit,
            offset,
            list: rows,
            total: count,
        });
    },

    async addCatagory(ctx) {
        const params = ctx.request.body;
        console.log(params);
        const validFlag = this.validCatagory(ctx, params);
        if (validFlag) {
            const poducts = await Catagory.create({
                ...params,
            });

            ctx.success('增加成功！', poducts);
        }
    },
    validCatagory(ctx, params) {
        if (!params.name) {
            ctx.errorParams('商品分类名称不存在！');
            return false;
        } else {
            return true;
        }
    },
    async deleteCatagory(ctx)  {
        const deleteId = ctx.request.body.id;
        if (!deleteId) {
            ctx.errorParams('id不存在！');
        } else {
            const length = await Catagory.destroy({
                where: {
                    id: deleteId,
                }
            });
            if (0 === length) {
                ctx.errorParams('数据库查询未找到！', {
                    id: deleteId,
                });
                return ;
            }
            await Products.update({
                catagoryName: '',
                catagoryId: null,
            },
            {
                where: {
                    catagoryId: deleteId,
                }
            });

            ctx.success('删除成功！', {
                id: deleteId,
            })
        }
        
    },
    async editCatagory(ctx)  {
        const params = ctx.request.body;
        const editId = params.id;
        if (!editId) {
            ctx.errorParams('id不存在！');
        } else {
            // 
            const length = await Catagory.update({
                name: params.name,
                parentId: params.parentId || '',
            },{
                where: {
                    id: params.id,
                }
            });
            console.log(length[0]);
            // 如果 更新的数据 不存在 阻断。
            if (0 === length[0]) {
                ctx.errorParams('数据库查询未找到！', {
                    id: params.id,
                });
                return ;
            };
            await Products.update({
                catagoryName: params.name,
            },
            {
                where: {
                    catagoryId: params.id,
                }
            });
            // const poducts = await Catagory.findOne({
            //     where: {
            //         id: editId
            //     },
            //     raw: true,
            // });
            // delete params.id;
            // await Catagory.update({
            //     ...poducts,
            //     ...params,
            // }, {
            //     where: {
            //         id: editId
            //     },
            // })
            
            ctx.success('修改成功', {
                id: editId,
            });
        }
        
    }
    
};


module.exports = {
    catagoryModel,
    Catagory
};